import java.lang.reflect.Field;

/**
 * @Authther: mrwu
 * @date:2021/3/4-03
 * @Description:PACKAGE_NAME
 */
public class Demo {

    public static void main(String[] args) throws Exception{
        //反射学习
        Class cls = Person.class;
        System.out.println(cls);
        //通过实例变量提供的getclass获取
        String s = "hello";
        Class cl = s.getClass();
        System.out.println(cl);
        //如知道class的完整类名 可以通过静态方法Class.forName()获取
        Class cl1 = Class.forName("java.lang.String");
        //因为Class实例在JVM中是唯一的 所以可以对上述
        System.out.println(cl==cl1);
        System.out.println(cls==cl);

//        pringObjectInfo(12);
//        pringObjectInfo("abc");
        Person p = new Person("xiao ming");
        System.out.println(p.name);
        Class c = p.getClass();
        Field f = c.getDeclaredField("name");
        // 获取属性
        String name = f.getName();
        System.out.println(name);
        //获取属性值

//        pringObjectInfo(p);
    }

   static void pringObjectInfo(Object obj){
        Class cls =obj.getClass();

        System.out.println(obj.getClass());
        //获取实例的信息
       System.out.println(cls.getFields());
    }
}
class Person {
    int age=0;
    String name ;
    public Person(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Person(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
}